//
//  LoginItemVew.m
//  Village
//
//  Created by 程虎 on 2017/10/15.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "LoginItemVew1.h"
#import "JKCountDownButton.h"

@interface LoginItemVew1 ()
@property (nonatomic, strong) UIButton *desBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) JKCountDownButton *codeButton;
@end

@implementation LoginItemVew1

- (instancetype)initWithItemsWithTypes:(NSInteger )types
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame  = CGRectMake(types * SCREEN_W, 0, SCREEN_W, 300);
        [self set_UITypes:types];
        
    }
    return self;
}

- (void)set_UITypes:(NSInteger )index
{
    [self creatPhoneNameView:nil Types:index];
}

- (void)creatUserNameView:(NSArray *)arrays Types:(NSInteger )index
{
    UIView *bkViews = [UIView new];
    bkViews.backgroundColor = [UIColor whiteColor];
    
    UIImageView *line = [UIImageView new];
    line.backgroundColor = K_LineColor;
    
    UIImageView *line1 = [UIImageView new];
    line1.backgroundColor = K_LineColor;
    
    UIImageView *line2 = [UIImageView new];
    line2.backgroundColor = K_LineColor;
    
    UILabel *lab = [[Tools shareInstance] creat_Label:@"账号" Color:[Util colorWithHexString:@"#333333"] Font:16 tag:-1 Alignment:0 Lines:1];
    UITextField *txtfield = [UITextField new];
    txtfield.font = LRFont(16);
    txtfield.borderStyle = UITextBorderStyleNone;
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs[NSForegroundColorAttributeName] = [Util colorWithHexString:@"#c0c0c0"]; // 设置颜色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:attrs]; // 初始化富文本占位字符串
    txtfield.attributedPlaceholder = attStr;
    
    UILabel *lab1 = [[Tools shareInstance] creat_Label:@"密码" Color:[Util colorWithHexString:@"#333333"] Font:16 tag:-1 Alignment:0 Lines:1];
    UITextField *txtfield1 = [UITextField new];
    txtfield1.font = LRFont(16);
    txtfield1.borderStyle = UITextBorderStyleNone;
    NSMutableDictionary *attrs1 = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs1[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs1[NSForegroundColorAttributeName] = [Util colorWithHexString:@"#c0c0c0"]; // 设置颜色
    NSAttributedString *attStr1 = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:attrs1]; // 初始化富文本占位字符串
    txtfield1.attributedPlaceholder = attStr1;
    
    UIButton *showPWBtn = [Tools creat_Buttons:@"" Color:[UIColor blackColor] BackColors:[UIColor whiteColor] Font:16 tag:30];
    showPWBtn.frame = CGRectMake(0, 1, 30, 28);
    
    showPWBtn.tag = 30;
    [showPWBtn addTarget:self action:@selector(showTextEntry:) forControlEvents:UIControlEventTouchUpInside];
    [showPWBtn setImage:[UIImage imageNamed:@"unopen"] forState:UIControlStateNormal];
    txtfield1.secureTextEntry = YES;
    txtfield1.rightView = showPWBtn;
    txtfield1.rightViewMode = UITextFieldViewModeAlways;
    
    UIButton *loginBtn = [Tools creat_Buttons:@"登录" Color:[UIColor whiteColor] BackColors:KRedColors Font:18 tag:203];
    [loginBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    LRViewRadius(loginBtn, 5, 0.5, [UIColor clearColor]);
    UIButton *desBtn = [[Tools shareInstance] creat_Button: @"使用短信验证码登录" Color:[Util colorWithHexString:@"#ff0000"] BackColors:[UIColor whiteColor] Font:14 tag:204];
    [desBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [bkViews sd_addSubviews:@[lab,txtfield,line,lab1,txtfield1,line1,loginBtn,desBtn]];
    [self addSubview:bkViews];

    CGFloat left = iPhone5 == YES ? 15 : 30;

    bkViews.sd_layout
    .leftSpaceToView(self, 0)
    .topSpaceToView(self, 0)
    .rightSpaceToView(self, 0);

    lab.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(bkViews, left)
    .heightIs(25);

    txtfield.sd_layout
    .leftSpaceToView(lab, 15)
    .centerYEqualToView(lab)
    .rightSpaceToView(bkViews, left)
    .heightIs(25);

    line.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield, 10)
    .rightSpaceToView(bkViews, left)
    .heightIs(0.5);

    lab1.sd_layout
    .leftEqualToView(lab)
    .topSpaceToView(line, left)
    .heightRatioToView(lab, 1);

    txtfield1.sd_layout
    .leftEqualToView(txtfield)
    .centerYEqualToView(lab1)
    .rightEqualToView(txtfield)
    .heightRatioToView(txtfield, 1);

    line1.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield1, 10)
    .rightSpaceToView(bkViews, left)
    .heightIs(0.5);

    loginBtn.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield1, 20)
    .rightSpaceToView(bkViews, left)
    .heightIs(45);

    desBtn.sd_layout
    .topSpaceToView(loginBtn, 20)
    .leftEqualToView(loginBtn)
    .rightEqualToView(loginBtn)
    .heightIs(40);

    [lab setSingleLineAutoResizeWithMaxWidth:150];
    [lab1 setSingleLineAutoResizeWithMaxWidth:150];

    [bkViews setupAutoHeightWithBottomView:desBtn bottomMargin:0];
}

- (void)creatPhoneNameView:(NSArray *)arrays Types:(NSInteger )index
{
    UIView *bkViews = [UIView new];
    bkViews.backgroundColor = [UIColor whiteColor];
    
    UIImageView *line = [UIImageView new];
    line.backgroundColor = K_LineColor;
    
    UIImageView *line1 = [UIImageView new];
    line1.backgroundColor = K_LineColor;
    
    UIImageView *line2 = [UIImageView new];
    line2.backgroundColor = K_LineColor;
    
    UILabel *lab = [[Tools shareInstance] creat_Label:@"手机号" Color:[Util colorWithHexString:@"#333333"] Font:16 tag:-1 Alignment:0 Lines:1];
    UITextField *txtfield = [UITextField new];
    txtfield.font = LRFont(16);
    txtfield.borderStyle = UITextBorderStyleNone;
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs[NSForegroundColorAttributeName] = [Util colorWithHexString:@"#c0c0c0"]; // 设置颜色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:attrs]; // 初始化富文本占位字符串
    txtfield.attributedPlaceholder = attStr;
    
    UILabel *lab1 = [[Tools shareInstance] creat_Label:@"验证码" Color:[Util colorWithHexString:@"#333333"] Font:16 tag:-1 Alignment:0 Lines:1];
    UITextField *txtfield1 = [UITextField new];
    txtfield1.font = LRFont(16);
    txtfield1.borderStyle = UITextBorderStyleNone;
    NSMutableDictionary *attrs1 = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs1[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs1[NSForegroundColorAttributeName] = [Util colorWithHexString:@"#c0c0c0"]; // 设置颜色
    NSAttributedString *attStr1 = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attrs1]; // 初始化富文本占位字符串
    txtfield1.attributedPlaceholder = attStr1;
    
    UIButton *showPWBtn = [Tools creat_Buttons:@"" Color:[UIColor blackColor] BackColors:[UIColor whiteColor] Font:16 tag:30];
    showPWBtn.frame = CGRectMake(0, 1, 30, 28);
    
    showPWBtn.tag = 30;
    [showPWBtn addTarget:self action:@selector(showTextEntry:) forControlEvents:UIControlEventTouchUpInside];
    [showPWBtn setImage:[UIImage imageNamed:@"unopen"] forState:UIControlStateNormal];
    txtfield1.secureTextEntry = YES;
    txtfield1.rightView = showPWBtn;
    txtfield1.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *lab2 = [[Tools shareInstance] creat_Label:@"短信验证码" Color:[Util colorWithHexString:@"#333333"] Font:16 tag:-1 Alignment:0 Lines:1];
    UITextField *txtfield2 = [UITextField new];
    txtfield2.font = LRFont(16);
    txtfield2.borderStyle = UITextBorderStyleNone;
    NSMutableDictionary *attrs2 = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs2[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs2[NSForegroundColorAttributeName] = [Util colorWithHexString:@"#c0c0c0"]; // 设置颜色
    NSAttributedString *attStr2 = [[NSAttributedString alloc] initWithString:@"请输入短信验证码" attributes:attrs2]; // 初始化富文本占位字符串
    txtfield.attributedPlaceholder = attStr2;
    
    showPWBtn.tag = 40;
    [showPWBtn addTarget:self action:@selector(showTextEntry:) forControlEvents:UIControlEventTouchUpInside];
    [showPWBtn setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:IMGCodeURL]]] forState:UIControlStateNormal];
    showPWBtn.frame = CGRectMake(0, 1, iPhone5 == YES ? 90 : 100, 28);
    txtfield2.rightView = showPWBtn;
    txtfield2.rightViewMode = UITextFieldViewModeAlways;
    
    self.codeButton = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    self.codeButton.frame = CGRectMake(0, 0, iPhone5 == YES ? 90 : 100, 30);
    self.codeButton.titleLabel.font = LRFont(12);
    self.codeButton.backgroundColor = [UIColor whiteColor];
    [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeButton setTitleColor:[Util colorWithHexString:@"#f54f33"] forState:UIControlStateNormal];
    LRViewRadius(self.codeButton, 3, 0.5, [Util colorWithHexString:@"#f54f33"]);
    self.codeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    txtfield2.rightView = self.codeButton;
    txtfield2.rightViewMode = UITextFieldViewModeAlways;
    
    __weak typeof (self) weakSelf = self;
    [self.codeButton countDownButtonHandler:^(JKCountDownButton *sender, NSInteger tag) {
//        UITextField *phoneTxt = [self viewWithTag:150 + self.indexType * 3];
//        if (![Tools checkPhoneNORationality:phoneTxt.text])
//        {
//            [MBProgressHUD showError:@"请输入正确的手机号！" toView:self];
//            [weakSelf.codeButton stopCountDown];
//        }
//        else {
//
//            [weakSelf loginSendVificationWithBtn:sender];
//        }
        
    }];
    
    UIButton *loginBtn = [Tools creat_Buttons:@"登录" Color:[UIColor whiteColor] BackColors:KRedColors Font:18 tag:203];
    [loginBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    LRViewRadius(loginBtn, 5, 0.5, [UIColor clearColor]);
    UIButton *desBtn = [[Tools shareInstance] creat_Button: @"使用短信验证码登录" Color:[Util colorWithHexString:@"#ff0000"] BackColors:[UIColor whiteColor] Font:14 tag:205];
    [desBtn addTarget:self action:@selector(otherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [bkViews sd_addSubviews:@[lab,txtfield,line,lab1,txtfield1,line1,lab,txtfield2,line2,loginBtn,desBtn]];
    [self addSubview:bkViews];
    
    CGFloat left = iPhone5 == YES ? 15 : 30;
    
    bkViews.sd_layout
    .leftSpaceToView(self, 0)
    .topSpaceToView(self, 0)
    .rightSpaceToView(self, 0);
    
    lab.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(bkViews, left)
    .heightIs(25);
    
    txtfield.sd_layout
    .leftSpaceToView(lab, 15)
    .centerYEqualToView(lab)
    .rightSpaceToView(bkViews, left)
    .heightIs(25);
    
    line.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield, 10)
    .rightSpaceToView(bkViews, left)
    .heightIs(0.5);
    
    lab1.sd_layout
    .leftEqualToView(lab)
    .topSpaceToView(line, left)
    .heightRatioToView(lab, 1);
    
    txtfield1.sd_layout
    .leftEqualToView(txtfield)
    .centerYEqualToView(lab1)
    .rightEqualToView(txtfield)
    .heightRatioToView(txtfield, 1);
    
    line1.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield1, 10)
    .rightSpaceToView(bkViews, left)
    .heightIs(0.5);
    
    loginBtn.sd_layout
    .leftSpaceToView(bkViews, left)
    .topSpaceToView(txtfield1, 20)
    .rightSpaceToView(bkViews, left)
    .heightIs(45);
    
    desBtn.sd_layout
    .topSpaceToView(loginBtn, 20)
    .leftEqualToView(loginBtn)
    .rightEqualToView(loginBtn)
    .heightIs(40);
    
    [lab setSingleLineAutoResizeWithMaxWidth:150];
    [lab1 setSingleLineAutoResizeWithMaxWidth:150];
    
    [bkViews setupAutoHeightWithBottomView:desBtn bottomMargin:0];
}


#pragma mark - 是否显示密码
- (void)showTextEntry:(UIButton *)buttons
{
    if (buttons.tag == 30) {
        buttons.selected = !buttons.selected;
        [buttons setImage:[UIImage imageNamed:buttons.selected == YES ? @"unopen" : @"open"] forState:UIControlStateNormal];
        UITextField *txtfield = [self viewWithTag:100];
        txtfield.secureTextEntry = buttons.selected;
    }
    else {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            UIImage *imgs = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.7.214:8071/APP/logic/va/123.jpg"]]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [buttons setBackgroundImage:imgs forState:UIControlStateNormal];
            });
        });
    }
}

- (void)otherBtnClick:(UIButton *)buttons
{
    if (self.scrollBlock) {
        self.scrollBlock(buttons.tag);
    }
}

@end
